package com.golddaymicromall.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.bean.LeaveMsgInfo;
import com.golddaymicromall.model.HttpModel;
import com.golddaymicromall.util.DateFormatUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.view.CyclerImg;


import java.sql.Date;
import java.text.SimpleDateFormat;

import java.util.List;

/**
 * Created by dawd on 2016/2/27.
 */
public class LeaveMsggAdapter extends BaseAdapter {
    private List<LeaveMsgInfo> list;
    private Context ctx;
    private String Username;
    private DateFormatUtils dateFormatUtils;
    private String headImgName;

    public LeaveMsggAdapter(List<LeaveMsgInfo> list, Context ctx) {
        this.list = list;

        this.ctx = ctx;
        Username = SPUtils.get(ctx, "Username", "").toString();
        headImgName = SPUtils.get(ctx, "headImg", "").toString();

    }


    @Override
    public int getCount() {
        return list.size();

    }

    @Override
    public Object getItem(int position) {
        return list.get(position);

    }

    @Override
    public long getItemId(int position) {
        return position;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder hold;
        if (convertView == null) {
            convertView = View.inflate(ctx, R.layout.item_leave_msgg, null);
            hold = new ViewHolder();
            hold.mneirong1 = (TextView) convertView.findViewById(R.id.neirong1);
            hold.mshijian = (TextView) convertView.findViewById(R.id.shijian);
            hold.mliuyan = (TextView) convertView.findViewById(R.id.liuyan);
            hold.mbianhao = (TextView) convertView.findViewById(R.id.bianhao);
            hold.leave_userhead = (CyclerImg) convertView.findViewById(R.id.leave_userhead);
            convertView.setTag(hold);
        } else {
            hold = (ViewHolder) convertView.getTag();
        }

        if (!"".equals(headImgName)) {
            Glide.with(ctx)
                    .load(new HttpModel().HEADIMGURL + headImgName)
                    .into(hold.leave_userhead);
        }
        if (list.get(position).getReplymessage()==null) {
            hold.mneirong1.setText("尚未回复");
        }else {
            hold.mneirong1.setText(list.get(position).getReplymessage());
        }

        hold.mliuyan.setText(list.get(position).getMessage());

        String getMessagedate = dateToString(list.get(position).getMessagedate());

        hold.mshijian.setText(getMessagedate);
//        hold.mbianhao.setText(list.get(position).getCustomerno());
        hold.mbianhao.setText(Username);
        return convertView;


    }

    public String dateToString(Date ctime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            String date = formatter.format(ctime);
            return date;
        } catch (Exception e) {

        }
        return null;
    }


    private class ViewHolder {
        TextView mneirong;
        TextView mneirong1;
        TextView mshijian;
        TextView mliuyan;
        TextView mname_tv;
        TextView mbianhao;
        TextView mzhidao;
        TextView mtheme;
        LinearLayout mLv2;
        CyclerImg leave_userhead;
    }
}
